package ugeOpgaver;

import java.util.Random;

public class Uge6Player implements Runnable {
	
	private boolean hasBall, game;
	private Uge6Player opponent;
	private String name;
	private int score;

	public Uge6Player(String name) {
		score=0;
		hasBall = false;
		game = true;
		this.name = name;
	}
	
	public void setOpponent(Uge6Player opponent){
		this.opponent = opponent;
	}
	
	public boolean hasBall(){
		return hasBall;
	}
	
	public void setHasBall(boolean hasBall){
		this.hasBall = hasBall;
	}
	
	public void setGame(boolean game){
		this.game = game;
	}
	
	public void wonPoint(){
		score++;
	}
	
	public int getScore(){
		return score;
	}
	
	public String getName(){
		return name;
	}
	
	@Override
	public void run() {
		while(game){
			Random r = new Random();
			if (hasBall){
				if(r.nextInt(10)+1 >= 2){
					System.out.println(name + " slår bolden over til " + opponent.getName());
					opponent.setHasBall(true);
					hasBall=false;
				} else {
					hasBall=false;
					opponent.wonPoint();
					System.out.println(name + " slog bolden i nettet! Point til " + opponent.getName() + " !");
				}
			} else {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
